package tgc.edu.exam.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.entity.Student;
import tgc.edu.exam.entity.SysFile;
import tgc.edu.exam.entity.TbClass;
import tgc.edu.exam.entity.VideoCourseware;
import tgc.edu.exam.service.StudentService;
import tgc.edu.exam.service.SysFileService;
import tgc.edu.exam.service.TbClassService;
import tgc.edu.exam.service.VideoCoursewareService;
import tgc.edu.exam.web.form.SysFileForm;
import tgc.edu.exam.web.form.VideoCoursewareForm;

@Controller
@RequestMapping(value = "/video")
public class VideoCoursewareController extends CommonController<VideoCourseware, UUID, VideoCoursewareForm> {

	@Autowired
	private VideoCoursewareService service;
	@Autowired
	private TbClassService tbClassSerivce;
	@Autowired
	private SysFileService sysFileService;

	@RequestMapping(value = "/details")
	public void details(Integer id, ModelMap map) {
		SysFile sysFile = sysFileService.findById(id);
		Specification<VideoCourseware> spec2 = buildSpec2(sysFile);
		List<VideoCourseware> list2 = service.findAll(spec2);
		Specification<TbClass> spec1 = buildSpec1(list2);
		List<TbClass> tbclass = tbClassSerivce.findAll(spec1);
		for (TbClass tbClass2 : tbclass) {
			VideoCourseware video = new VideoCourseware();
			video.setSysfile(sysFile);
			video.setTbclass(tbClass2);
			video.setIsstart(true);
			service.save(video);
		}
		List<VideoCourseware> list = service.findAll(spec2);
		map.put("data", list);
	}
	
	@RequestMapping(value = "/details1")
	public void details1(Integer id, ModelMap map) {
		SysFile sysFile = sysFileService.findById(id);
		Specification<VideoCourseware> spec2 = buildSpec2(sysFile);
		List<VideoCourseware> list2 = service.findAll(spec2);
		Specification<TbClass> spec1 = buildSpec1(list2);
		List<TbClass> tbclass = tbClassSerivce.findAll(spec1);
		for (TbClass tbClass2 : tbclass) {
			VideoCourseware video = new VideoCourseware();
			video.setSysfile(sysFile);
			video.setTbclass(tbClass2);
			video.setIsstart(true);
			service.save(video);
		}
		List<VideoCourseware> list = service.findAll(spec2);
		map.put("data", list);
	}

	@RequestMapping(value="/tijiao")
	@ResponseBody
	public String tijiao(boolean isstart,String id) {
		try {
			VideoCourseware video = service.findById(UUID.fromString(id));
			video.setIsstart(isstart);
			service.save(video);
			return "操作完成";
		} catch (Exception e) {
			return "操作失败";
		}	
	}
	
	
	private Specification<VideoCourseware> buildSpec2(SysFile sysFile) {
		Specification<VideoCourseware> specification = new Specification<VideoCourseware>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<VideoCourseware> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				rules.add(cb.equal(root.get("sysfile"), sysFile));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}




	public Specification<TbClass> buildSpec1(List<VideoCourseware> list2) {
		Specification<TbClass> specification = new Specification<TbClass>() {

			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<TbClass> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				for (VideoCourseware video : list2) {
					rules.add(cb.notEqual(root.get("id"), video.getTbclass().getId()));
				}
				rules.add(cb.like(root.get("classStatus").get("state"), "%开班%"));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}

		};
		return specification;
	}

}
